var global_wait_msgs = [];

function getWaitMsgs(){
    var ids = global_wait_msgs;
    for(var i in ids){
        var msg_id = ids[i];
        $.ajax({
            url:api_path+'msg/getRpley',
            data:{id:msg_id},
            type:'post',
            dataType:'json',
            success:function(response){
              if (response.code == 2000) {
                var data = response.data;
                if (data.type == 'htsc') {
                    var str = createHtscReplyHtml(data.reply,data.rtime);    
                }else if(data.type == 'aidraw'){
                    var str = createAidrawReplyHtml(data.reply,data.rtime);
                }
                
                $("#chatContent").append(str);
                global_wait_msgs.splice(i,1);

                scrollBottom();
              }
            }
        });
    }
    setTimeout(function() {
        getWaitMsgs();
    }, 5000);
}

function sendHtsc(){
    var params = new FormData();

    var files = $("#htsc_file")[0].files;
    params.append('file',files[0]);
    params.append('type',$("input[name='htsc_type']:checked").val());
    params.append('session_sn',global_assistant.session_sn);

    var l = $('body').lyearloading({
        opacity: 0.5,
        spinnerSize: 'lg',
        spinnerText: '合同上传中，请稍后',
        textColorClass: 'text-info',
        spinnerColorClass: 'text-info'
    });
    
    $.ajax({
        url:api_path+'msg/htsc',
        data:params,
        type:'post',
        contentType:false,
        processData:false,
        success:function(response){
          var data = response.data;
          if (response.code == '2000' || response.code == '2001') {
            var str = createHtscMessageHtml(data.message,data.ctime);
            str += createMsgHtml(data.reply,1,data.rtime);

            $("#chatContent").append(str);
            $("#createGroup").modal('hide');

            if (response.code == '2000') {
                global_wait_msgs.push(response.msg_id);
            }
            scrollBottom();
          }else{
            cchUtil.message(response.msg,'danger');
          }
          
          l.hide();
        }
    });
}

// 生成对话HTML
function createHtscMessageHtml(message,ctime){
    if (ctime == null || ctime == '') {
        ctime = getFamtterHours();
    }
    var msg_class = 'message self';
    var avatar = upload_path+'/'+global_user.headimgurl;

    var download_btns = '<a target="_blank" style="color:#ffb549;margin-right:20px;" href="https://falvai.bj.bcebos.com/'+message.url+'"><i class="mdi mdi-download"></i>下载源文档</a>';
    if (message.report_url != null) {
        download_btns += '<a target="_blank" style="color:#fff;" href="'+message.report_url+'"><i class="mdi mdi-download"></i>下载结果文档</a>';
    }


    var str = '<div class="'+msg_class+'">'+
                '<div class="message-wrapper">'+
                    '<div class="message-content">'+
                        '<h6>合同审查</h6>'+
                        '<h6>文件名称：'+message.name+'</h6>'+
                        '<h6>合同类型：'+(message.type == 'Sales_PartyA' ? '买卖合同审查-甲方立场' : '买卖合同审查-乙方立场')+'</h6>'+
                        download_btns+
                        // '<a class="popup-media" href="'+static_path+'/index/htsc_file.png">'+
                        //     '<img class="img-fluid rounded" src="'+static_path+'/index/htsc_file.png" alt="" style="width:100px;">'+
                        // '</a>'+
                    '</div>'+
                '</div>'+
                '<div class="message-options">'+
                    '<div class="avatar avatar-sm"><img alt="" src="'+avatar+'"></div>'+
                    '<span class="message-date">'+ctime+'</span>'+
                    '<div class="dropdown">'+
                        '<a class="text-muted" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'+
                            '<svg class="hw-18" fill="none" viewBox="0 0 24 24" stroke="currentColor">'+
                                '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h.01M12 12h.01M19 12h.01M6 12a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0z"/>'+
                            '</svg>'+
                        '</a>'+

                        '<div class="dropdown-menu">'+
                            '<a class="dropdown-item d-flex align-items-center copy_btn" href="https://falvai.bj.bcebos.com/'+message.url+'" download="'+message.name+'">'+
                                '<svg class="hw-18 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">'+
                                    '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>'+
                                '</svg>'+
                                '<span>下载</span>'+
                            '</a>'+
                        '</div>'+
                    '</div>'+
                '</div>'+
            '</div>';

    return str;
}

// 生成对话HTML
function createHtscReplyHtml(message,ctime){
    if (message == null || message == '') {
      return '';
    }
    if (ctime == null || ctime == '') {
        ctime = getFamtterHours();
    }
    var msg_class = 'message msg_htsc';
    var avatar = upload_path+'/'+global_assistant.image;

    var str = '<div class="'+msg_class+'">'+
                '<div class="message-wrapper">'+
                    '<div class="message-content">'+
                        '<span>'+message+'</span>'+
                    '</div>'+
                '</div>'+
                '<div class="message-options">'+
                    '<div class="avatar avatar-sm"><img alt="" src="'+avatar+'"></div>'+
                    '<span class="message-date">'+ctime+'</span>'+
                '</div>'+
            '</div>';

    return str;
}

// 合同审查手风琴展开
function clickAccordionItemButton(btn){
    if ($(btn).attr('aria-expanded') == 'true') {
        $(btn).parent().parent().find('.card').addClass('show');
    }else{
        $(btn).parent().parent().find('.card').removeClass('show');
        // $($(".accordion-button[aria-expanded='true']")[0]).parent().parent().find('.card').removeClass('collapse');
    }
}